// void, Obj This

Hero this;
str names;

Sleep(rand(50));

this = This.AsHero;

while(!.IsValid) Sleep(10000); 

while(.IsValid){
	if(IsAIPlayer(.player)){
		//while(.IsValid) 
		while(1) Sleep(3*60000);
		
	} else {

		names = "/1/AutoFeeding/"+.name;       
		if(EnvReadString(names) != "yes"){
			if(.army.count > 10){
				if(!.InShip())
				if(.command == "stand_position" || .command == "idle"){	                
					ObjList olWagon, olFoodWagon; 
					Unit u;
					int i, fw, am;
					
					am = 0;
					if(.InHolder){
						Settlement sett;
						
						sett = .GetHolderSett;
						if(sett.food < 1000){
							for(i=0; i<sett.Units.count; i+=1) {
								
								u = .GetHolderSett.Units[i].AsUnit;
								if(u.IsValid){
									if(u.IsHeirOf("Wagon")){
										if(u.AsWagon.restype == 1){
											olFoodWagon.Add(u.AsWagon);
											am += u.AsWagon.amount;
										}
									}
								}
							} 
						} else { am = sett.food; }
					} else {
						olWagon = Intersect(ObjsInCircle(.posRH, 800, "Wagon"), ClassPlayerObjs("Wagon", .player)).GetObjList();
						olWagon.ClearDead;
						for(i=0; i<olWagon.count; i+=1) {
							
							u = olWagon[i].AsUnit;
							if(u.IsValid){
								if(u.AsWagon.restype == 1){
									olFoodWagon.Add(u.AsWagon);
									am += u.AsWagon.amount;
								}
							}
						}
					}
					//pr(olFoodWagon.count);
					olFoodWagon.ClearDead;
					fw = olFoodWagon.count;
					if(fw >= 6 && am < 2000) {
						Wagon w1, w2;
						w1 = olFoodWagon[0].AsWagon;
						w2 = olFoodWagon[1].AsWagon;
						if(w1.IsValid && w2.IsValid){
							w2.SetFood(w2.amount + w1.amount);
							w1.Damage(100000);
						}
					}
					if(fw <= 5 && am < 1000){
						Query qBuilding, qBT, qOutpost, qShipyard;
						ObjList olBuilding, olBuildingFood;
						Settlement sett, sett2;
						Building bld;
						int f, f_max, f_percent, bldFoodCount, d, disti;
						IntArray dist;
						point ptBld, ptHero;
															
						qBT = ClassPlayerObjs("BaseTownhall", .player);
						qOutpost = ClassPlayerObjs("Outpost", .player);
						qShipyard = ClassPlayerObjs("BaseShipyard", .player);
						qBuilding = Union(qBT, qOutpost);
						qBuilding = Union(qBuilding, qShipyard);
						olBuilding = qBuilding.GetObjList();
						
						for(i=0; i<olBuilding.count; i+=1) {
							
							bld = olBuilding[i].AsBuilding;
							sett2 = bld.settlement;
							if(sett2.IsValid)
							if(sett2.food >= 1000)
								olBuildingFood.Add(bld);
						}
						bldFoodCount = olBuildingFood.count;
						for(i=0; i<bldFoodCount; i+=1) {
							
							bld = olBuildingFood[i].AsBuilding;
							if(bld.IsValid){
								ptBld = bld.pos;
								ptHero = .posRH;
								dist[i] = ptBld.Dist(ptHero);
							}
						}
						if(bldFoodCount == 1){
							d = dist[0];
						} else if(bldFoodCount == 2){
							d = MIN(dist[0], dist[1]);
						} else if(bldFoodCount == 2){
							d = MIN(dist[0], dist[1]);
						} else if(bldFoodCount == 3){
							d = MIN(dist[0], MIN(dist[1], dist[2]));
						} else if(bldFoodCount == 4){
							d = MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3]));
						} else if(bldFoodCount == 5){
							d = MIN(MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3])), dist[4]);
						} else if(bldFoodCount == 6){
							d = MIN(MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3])), MIN(dist[4], dist[5]));
						} else if(bldFoodCount == 7){
							d = MIN(MIN(MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3])), MIN(dist[4], dist[5])), dist[6]);
						} else if(bldFoodCount == 8){
							d = MIN(MIN(MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3])), MIN(dist[4], dist[5])), MIN(dist[6], dist[7]));
						} else if(bldFoodCount == 9){
							d = MIN(MIN(MIN(MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3])), MIN(dist[4], dist[5])), MIN(dist[6], dist[7])), dist[8]);
						} else if(bldFoodCount >= 10){
							d = MIN(MIN(MIN(MIN(MIN(dist[0], dist[1]), MIN(dist[2], dist[3])), MIN(dist[4], dist[5])), MIN(dist[6], dist[7])), MIN(dist[8], dist[9]));
						}
						
						for(i=0; i<bldFoodCount; i+=1) {
							
							bld = olBuildingFood[i].AsBuilding;
							if(bld.IsValid){
								ptBld = bld.pos;
								ptHero = .posRH;
								disti = ptBld.Dist(ptHero);
								if(d == disti){
									sett = olBuildingFood[i].AsBuilding.settlement;
									break;
								} else {
									sett = olBuildingFood[0].AsBuilding.settlement;
								}
							}
						}
						
						if(sett.IsValid){
							if(sett.food >= 1000){  
								Wagon wagon;
								int d, j, c, border;
										   
								d = .DistTo(sett.GetCentralBuilding.pos);
								.army.ClearDead;
								for (i=0; i<.army.count; i+=1){
									f += .army[i].AsUnit.food; 
									f_max += .army[i].AsUnit.maxfood;
								}
								
								f_percent = f*100/f_max;
								border = 10;
								//pr(f_percent);
								if(d >= 20000){
												
									border = 48;
									if(f_percent < border){
										
										for(j=0; j<2; j+=1){
											
											wagon = sett.CreateMuleFood(.army.count*20/2+400);
											if(wagon.IsValid){
												
												wagon.SetCommand("follow",this);
												wagon.AddCommand(false, "follow",this);
												wagon.AddCommand(false, "unload", sett.GetCentralBuilding());                                                                               
												
												Sleep(800);        
											}
										}
									}  
								} else if(d >= 15000){
												
									border = 46;  
									if(f_percent < border){
										
										for(j=0; j<2; j+=1){
											 
											wagon = sett.CreateMuleFood(.army.count*20/2+300);
											if(wagon.IsValid){
												
												wagon.SetCommand("follow",this);
												wagon.AddCommand(false, "follow",this);
												wagon.AddCommand(false, "unload", sett.GetCentralBuilding());                                                                                  
												
												Sleep(800);        
											}
										}
									} 
								} else if(d >= 10000){
												
									border = 42; 
									if(f_percent < border){
										
										for(j=0; j<2; j+=1){ 
											wagon = sett.CreateMuleFood(.army.count*20/2+200);
											if(wagon.IsValid){
												
												wagon.SetCommand("follow",this); 
												wagon.AddCommand(false, "follow",this);
												wagon.AddCommand(false, "unload", sett.GetCentralBuilding());                                                                                  
										
												Sleep(800);        
											}
										}
									} 
								} else if(d >= 1000){
												
									border = 32;
									if(f_percent < border){
										
										wagon = sett.CreateMuleFood(.army.count*20+100);
										if(wagon.IsValid){
											
											wagon.SetCommand("follow",this);
											wagon.AddCommand(false, "follow",this); 
											wagon.AddCommand(false, "unload", sett.GetCentralBuilding());                                                                                  
										}
										
									}
								}
								
								while(f_percent < border){
									.army.ClearDead;
									for (i=0; i<.army.count; i+=1){
										f += .army[i].AsUnit.food; 
										f_max += .army[i].AsUnit.maxfood;
									}
									
									f_percent = f*100/f_max;
									Sleep(5000);
									c += 1;
									if(c > 11) break;
								}
							} 
						}
				   
					}
				} 
			} else {
				if(.food < 1 && .health < .maxhealth){
					ObjList olBT;
					Settlement sett;
																
					olBT = ClassPlayerObjs("BaseTownhall", .player).GetObjList();
					sett = olBT[0].AsBuilding.settlement;
					if(sett.IsValid){
						if(sett.food >= 500){
							Wagon wagon;
							wagon = sett.CreateMuleFood(500);
							if(wagon.IsValid){
								wagon.SetCommand("follow",this);  
								wagon.AddCommand(false, "unload", sett.GetCentralBuilding());        
							}
							
							while(.food < 1 && .health < .maxhealth) Sleep(3000);
				
						}
					}
				}
			}
		}
	}
	Sleep(10000+rand(100));
}         
